package com.ruoyi.beforehand.service.impl;

import java.io.File;
import java.util.*;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.attachment.domain.Attachment;
import com.ruoyi.attachment.mapper.AttachmentMapper;
import com.ruoyi.attachment.service.IAttachmentService;
import com.ruoyi.attachment.service.impl.AttachmentServiceImpl;
import com.ruoyi.beforehand.domain.InfoBeforehandEvaluateDetail;
import com.ruoyi.beforehand.domain.InfoScoreVo;
import com.ruoyi.beforehand.mapper.InfoBeforehandEvaluateDetailMapper;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.evaluate.infoprojectevaluate.domain.InfoProjectEvaluate;
import com.ruoyi.monitor.infoprojectmonitor.domain.InfoProjectMonitor;
import com.ruoyi.performance.domain.InfoPrePerformanceEvaluation;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.beforehand.mapper.InfoBeforehandEvaluateMapper;
import com.ruoyi.beforehand.domain.InfoBeforehandEvaluate;
import com.ruoyi.beforehand.service.IInfoBeforehandEvaluateService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 事前评估信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-02-24
 */
@Service
public class InfoBeforehandEvaluateServiceImpl implements IInfoBeforehandEvaluateService
{
    @Resource
    private InfoBeforehandEvaluateMapper infoBeforehandEvaluateMapper;

    @Resource
    private InfoBeforehandEvaluateDetailMapper infoBeforehandEvaluateDetailMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysDeptMapper sysDeptMapper;
    @Resource
    private AttachmentMapper attachmentMapper;
    @Resource
    private IAttachmentService attachmentService;
    /**
     * 查询事前评估信息
     *
     * @param id 事前评估信息主键
     * @return 事前评估信息
     */
    @Override
    public InfoBeforehandEvaluate selectInfoBeforehandEvaluateById(Long id)
    {
        InfoBeforehandEvaluate infoBeforehandEvaluate = infoBeforehandEvaluateMapper.selectInfoBeforehandEvaluateById(id);
        //根据事前评估对象id查询附件
        List<Attachment> attachmentList = attachmentMapper.selectAttachmentListById(infoBeforehandEvaluate.getId(),"info_beforehand_evaluate");
        if (!attachmentList.isEmpty()) {
            List<Map> supportList = new ArrayList();
            for (Attachment attachment : attachmentList) {
                HashMap<String, Object> supportMap = new HashMap<>();
                supportMap.put("attachmentId",attachment.getAttachmentId());
                supportMap.put("initialAttachmentName",attachment.getInitialAttachmentName());
                supportMap.put("attachmentUrl",attachment.getAttachmentUrl());
                supportMap.put("remark",attachment.getRemark());
                supportList.add(supportMap);
            }
            infoBeforehandEvaluate.setSupportFile(supportList);
            return infoBeforehandEvaluate;
        }
        return infoBeforehandEvaluate;
    }

    /**
     * 查询事前评估信息列表
     *
     * @param infoBeforehandEvaluate 事前评估信息
     * @return 事前评估信息
     */
    @Override
    public List<InfoBeforehandEvaluate> selectInfoBeforehandEvaluateList(InfoBeforehandEvaluate infoBeforehandEvaluate)
    {
        return infoBeforehandEvaluateMapper.selectInfoBeforehandEvaluateList(infoBeforehandEvaluate);
    }

    /**
     * 新增事前评估信息
     *
     * @param infoBeforehandEvaluate 事前评估信息
     * @return 结果
     */
    @Override
    public int insertInfoBeforehandEvaluate(InfoBeforehandEvaluate infoBeforehandEvaluate)
    {
        return infoBeforehandEvaluateMapper.insertInfoBeforehandEvaluate(infoBeforehandEvaluate);
    }

    /**
     * 修改事前评估信息
     *
     * @param infoBeforehandEvaluate 事前评估信息
     * @return 结果
     */
    @Override
    //事务回滚
    @Transactional(rollbackFor = Exception.class)
    public int updateInfoBeforehandEvaluate(InfoBeforehandEvaluate infoBeforehandEvaluate)
    {
        /**
         *待填报 2 再审核3（通过）4（驳回）
         */
//            if("3".equals(infoBeforehandEvaluate.getBeforehandStatus())){
//                InfoBeforehandEvaluateDetail infoBeforeHandEvaluateDetail=new InfoBeforehandEvaluateDetail();
//                List<InfoBeforehandEvaluateDetail> infoBeforehandEvaluateDetails= infoBeforehandEvaluateDetailMapper.selectInfoBeforehandEvaluateDetailsById(infoBeforeHandEvaluateDetail.getBeforehandId());
//                if(infoBeforehandEvaluateDetails.isEmpty()){
//                    throw new RuntimeException("评估信息为空");
//                }
//            }
        infoBeforehandEvaluateMapper.updateInfoBeforehandEvaluate(infoBeforehandEvaluate);
        return 1;
    }

    /**
     * 批量删除事前评估信息
     *
     * @param ids 需要删除的事前评估信息主键
     * @return 结果
     */
    @Override
    public int deleteInfoBeforehandEvaluateByIds(Long[] ids)
    {
        return infoBeforehandEvaluateMapper.deleteInfoBeforehandEvaluateByIds(ids);
    }

    /**
     * 删除事前评估信息信息
     *
     * @param id 事前评估信息主键
     * @return 结果
     */
    @Override
    public int deleteInfoBeforehandEvaluateById(Long id)
    {
        return infoBeforehandEvaluateMapper.deleteInfoBeforehandEvaluateById(id);
    }

    @Override
    public List<InfoBeforehandEvaluate> selectInfoBeforehandEvaluatesById(Long id) {
        return infoBeforehandEvaluateMapper.selectInfoBeforehandEvaluatesById(id);
    }

    @Override
    public int updateInfoBeforehandEvaluatess(InfoScoreVo infoScoreVo) {
        //查
        Long id=infoScoreVo.getBeforehandId();
        InfoBeforehandEvaluateDetail infoBeforehandEvaluateDetail=infoBeforehandEvaluateDetailMapper.selectInfoBeforehandEvaluateDetailById(id);
        //修改分数
        infoBeforehandEvaluateDetail.setScore(infoScoreVo.getScore());
        infoBeforehandEvaluateDetailMapper.updateInfoBeforehandEvaluateDetail(infoBeforehandEvaluateDetail);
        return 1;
    }

    @Override
    public void updateInfoBeforehandEvaluates(InfoBeforehandEvaluate infoBeforehandEvaluate) {
        infoBeforehandEvaluateMapper.updateInfoBeforehandEvaluates(infoBeforehandEvaluate);
    }

    @Override
    public List<InfoBeforehandEvaluate> selectInfoBeforehandEvaluateLists(InfoBeforehandEvaluate infoBeforehandEvaluate) {
        //当前登录人
        SysUser nowLoginUser = SecurityUtils.getLoginUser().getUser();
        //查询主管处室
        SysUser sysUser=sysUserMapper.selectUserByUserNames(nowLoginUser);
        List<String> rileKeys=  sysUser.getRoles().stream().map(SysRole::getRoleKey).collect(Collectors.toList());
        Long deptId=sysUser.getDeptId();
        if(rileKeys.contains("sjc")){
            infoBeforehandEvaluate.setDeptId(deptId);
        }else{
            infoBeforehandEvaluate.setDepartmentId(deptId);
        }
        return infoBeforehandEvaluateMapper.selectInfoBeforehandEvaluatesByIds(infoBeforehandEvaluate);

    }

    @Override
    public List<InfoBeforehandEvaluate> selectInfoBeforehandEvaluateListss(InfoBeforehandEvaluate infoBeforehandEvaluate) {
        //当前登录人
        SysUser nowLoginUser = SecurityUtils.getLoginUser().getUser();
        //查询部门
        SysUser sysUser=sysUserMapper.selectUserByUserNames(nowLoginUser);
        Long deptId=sysUser.getDeptId();
        SysDept sysDept=sysDeptMapper.selectDeptById(deptId);
        //获取预算单位
        String dept=sysDept.getDeptName();

        return infoBeforehandEvaluateMapper.selectInfoBeforehandEvaluateLists(dept);
    }

    @Override
    public int uploadFiles(InfoBeforehandEvaluate infoBeforehandEvaluate) {
        Long id=infoBeforehandEvaluate.getId();
        //读取这个文件地址
        String fileUrl = infoBeforehandEvaluate.getReportUrl();
        String fileName=infoBeforehandEvaluate.getReportName();
        InfoBeforehandEvaluate infoBeforehandEvaluate1=infoBeforehandEvaluateMapper.selectInfoBeforehandEvaluateById(id);
        infoBeforehandEvaluate1.setReportUrl(fileUrl);
        infoBeforehandEvaluate1.setReportName(fileName);
        int i=infoBeforehandEvaluateMapper.updateInfoBeforehandEvaluate(infoBeforehandEvaluate1);
        return i;
    }

    /**
     * 附件上传
     * @param attachment
     * @return
     */
    @Override
    public int uploadMuchFiles(Attachment attachment,Integer type) {
        //前端传过来 事前评估id 初始文件名 附件url 备注
        //从url中截取生成的新名字
        String substring = attachment.getAttachmentUrl().substring(attachment.getAttachmentUrl().lastIndexOf("/") + 1);
        attachment.setAttachmentName(substring);
        //设置附件类型
        attachment.setAttachmentType("1");
        if(type.equals(1)){
            attachment.setTableName("info_beforehand_evaluate");
        }else{
            Attachment att=new Attachment();
            att.setInfoDetailId(attachment.getInfoDetailId());
            att.setInfoId(attachment.getInfoId());
            List<Attachment> attachments=attachmentMapper.selectAttachmentList(att);
            if(attachments.size()>0){
                Attachment attach= attachments.get(0);
                //删除服务器附件
                attachmentService.deleteServerFile(attach.getAttachmentUrl());
                //删除数据库附件
                attachmentMapper.deleteAttachmentByAttachmentId(attach.getAttachmentId());
            }
            attachment.setTableName("info_beforehand_evaluate_detail");
        }
        //设置创建人
        attachment.setCreateUser(SecurityUtils.getUserId());
        //创建时间
        attachment.setCreateTime(new Date());
        return attachmentMapper.insertAttachment(attachment);
    }

    /**
     * 删除附件
     * @param attachmentId
     */
    @Override
    public void deleteAttachmentByAttachmentId(Long attachmentId) {
        attachmentMapper.deleteAttachmentByAttachmentId(attachmentId);
    }


}
